export default {
  data() {
    return {
      ws: null,
    };
  },
  methods: {
    connectWebSocket() {
      this.ws = new WebSocket('ws://192.168.1.3:2346?uid=100');

      this.ws.onopen = this.onOpen;
      this.ws.onmessage = this.onMessage;
      this.ws.onerror = this.onError;
      this.ws.onclose = this.onClose;
    },
    onOpen() {
      console.log('WebSocket连接成功');
    },
    onMessage(event) {
      console.log('收到消息:', event.data);
    },
    onError(error) {
      console.error('WebSocket出错:', error);
    },
    onClose() {
      console.log('WebSocket连接已关闭');
    },
    sendMessage() {
      if (this.ws && this.ws.readyState === WebSocket.OPEN) {
        this.ws.send('你的消息内容');
      } else {
        console.error('WebSocket连接未打开');
      }
    }
  },
  beforeDestroy() {
    if (this.ws) {
      this.ws.close();
    }
  }
};